<?php
namespace app\api\service\uniapp\order;

use app\api\controller\Huada;
use app\api\controller\Leshua;
use app\api\controller\uniapp\Order;
use app\api\model\Region;
use app\api\model\ShopPayOrder;
use app\api\model\ShopYiwu;
use app\api\model\Users;
use app\api\model\UserStore;
use app\common\controller\Api;
use think\Db;
use think\Response;
use addons\qrcode\library\Service as QrcodeService;
use function Sodium\add;

class Order10 extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function load_order(){
        //接收参数
        $user = session('authuser');
        $type = $this->request->param('type');
        $current = $this->request->param('current');
        $page = $this->request->param('page',1);
        $size = $this->request->param('size',20);
        $page = ($page-1)*$size;

        if($current==0){
            $swhere = "1=1";
        }else if($current==1){
            $swhere = "status=0";
        }else if($current==2){
            $swhere = "status=1";
        }else if($current==3){
            $swhere = "status=3";
        }

        if($type==1){ //易物订单
            $orderlist = ShopPayOrder::where(['user_id'=>$user['user_id'],'order_type'=>7])->where($swhere)->limit($page,$size)->order('create_time','desc')->select();
        }else if($type==2){ //扫码单
            $orderlist = ShopPayOrder::where(['user_id'=>$user['user_id'],'order_type'=>1])->where($swhere)->limit($page,$size)->order('create_time','desc')->select();
        }else if($type==3){ //普通单
            $orderlist = null;
        }

        $this->success('加载成功',$orderlist);
    }

    public function get_order(){
        //接收参数
        $user = session('authuser');
        $order_id = $this->request->param('order_id');
        $order = ShopPayOrder::where(['user_id'=>$user['user_id'],'id'=>$order_id])->with('shop')->find();
        if(!$order){
            $this->error("订单不存在");
        }
        $params['text'] = config('site_url').'api/uniapp/order/verification?code='.$order['hexiao_code'];
        $qrCode = QrcodeService::qrcode($params);

        $qrcodePath = 'uploads/qrcode/';
        if (!is_dir($qrcodePath)) {
            @mkdir($qrcodePath);
        }
        if (is_really_writable($qrcodePath)) {
            $filePath = $qrcodePath . md5(implode('', $params)) . '.' . "png";
            $qrCode->saveToFile($filePath);
        }
        $order['qrcode'] = config('img_url').$filePath;
        $this->success('加载成功',$order);
    }

    public function verification(){
        //接收参数
        $user = session('authuser');
        $code = $this->request->param('code');
        if(!$code){
            $this->error("核销码不能为空");
        }
        if(!is_numeric($code)){
            $this->error("核销码只能为纯数字");
        }
        $order = ShopPayOrder::where('hexiao_code',$code)->find();
        if(!$order){
            $this->error("核销码不存在");
        }
        if ($order['shop_user_id']!=$user['user_id'] && $order['shop_user_id']!=$user['shop_account_id']){
            $this->error("不是您店铺的订单");
        }
        if($order['status']!=1){
            $this->error('核单不是已支付状态');
        }
        $orderService = new OrderService();
        //加载消费券订单
        $ywqorder = $orderService->getOrderByTotalOrderId($order['id'],6);
        if($ywqorder){
            new_award($ywqorder['id']);
        }
        //加载现金订单
        $xjorder = $orderService->getOrderByTotalOrderId($order['id'],1);
        if($xjorder){
            new_award($xjorder['id']);
            //分账
            order_multi_apply($xjorder['id']);
        }
    }

    public function createShoppingOrder(){
        $user = session('authuser');
        //接收参数
        $goodsid = $this->request->param('goodsid');
        $pay_sum = $this->request->param('pay_sum');
        $total_price = $this->request->param('total_price');
        $address_id = $this->request->param('address_id',0);

        $orderService = new OrderService();
        $res = $orderService->createShoppingOrder($user['user_id'],$goodsid,$pay_sum,$total_price,$address_id);
        return json_encode($res);
    }

    public function load_all_order(){
        //接收参数
        $ordersn = $this->request->param('ordersn');
        $orderServie = new OrderService();
        //获取现金订单
        $xjorder = $orderServie->getOrderBySn($ordersn);
        if(!$xjorder){
            $this->error("订单不存在");
        }
        //获取总订单
        $totalorder = $orderServie->getOrderById($xjorder['total_order_id']);
        //获取消费券订单
        $ywqorder = $orderServie->getOrderByTotalOrderId($xjorder['total_order_id'],6);
        $order['totalorder'] = $totalorder;
        $order['xjorder'] = $xjorder;
        $order['ywqorder'] = $ywqorder;

        $this->success('加载成功',$order);
    }

    public function gopay(){
        //接收参数
        $ordersn = $this->request->param('ordersn');
        $benefit_type = $this->request->param('benefit_type');
        $orderServie = new OrderService();
        //获取现金订单
        $xjorder = $orderServie->getOrderBySn($ordersn);
        if(!$xjorder){
            $this->error("订单不存在");
        }
        //保存订单福利类型
        $xjorder->benefit_type = $benefit_type;
        $xjorder->save();
        //保存总订单金额类型
        $ywqorder = $orderServie->getOrderByTotalOrderId($xjorder['total_order_id'],6);
        $totalorder = $orderServie->getOrderById($xjorder['total_order_id']);
        if($ywqorder){
            $totalorder->money_type = 11;
        }else{
            $totalorder->money_type = 1;
        }
        $totalorder->save();

        $leshua = new Leshua();
        $leres = $leshua->apppay($ordersn,$xjorder['order_money'],$xjorder['user_id'],1);
        if($leres['status']){
            $res['code']=1;
            $res['data'] = $leres['jspay_info'];
            $res['msg'] = "发起支付";
        }else{
            $res['code'] = -6;
            $res['msg'] = $leres['error_msg'];
        }
        return json_encode($res);
    }
}